#include "PrecompiledHeader_Test.h"
#include "SimpleGame.h"

/*--------------------------------------------------------------------------*/
#define swiatelka 200
SimpleGame::SimpleGame() :
	ICalc(CCalc::MultiThreadHighPriority)
{
	//this->walls = new MeshInstance*[100];
	this->walls = new RenderableVao*[100];

	Mesh* house = NULL;
	CResources::getInstance()->Get("house5", &house);

	for (int x = 0; x < 10; x++)
	{
		for (int y = 0; y < 10; y++)
		{
			std::stringstream ss;
			ss << "ball_" << x << "_" << y;
			this->walls[y + 10 * x] = new RenderableVao();
			this->walls[y + 10 * x]->SetHighQualityVao(OpenGL::GetSmallSphereVao());
			this->walls[y + 10 * x]->SetLowQualityVao(OpenGL::GetSmallSphereVao());

			//this->walls[y + 10 * x] = house->CreateInstance();//new Mesh("../Data/model/house5.3ds");
			this->walls[y + 10 * x]->SetName(ss.str());
			this->walls[y + 10 * x]->SetPosition(glm::vec3(x * 30.0f, 0.0f, y * 30.0f));
			this->walls[y + 10 * x]->SetRadius(12.0f);
			this->walls[y + 10 * x]->SetParent(CTree::getInstance()->GetRoot());
			this->walls[y + 10 * x]->SetScale(15.0f);
			this->walls[y + 10 * x]->SetAllowObjectTextures(false);
		}
	}

	RenderableVao* r = new RenderableVao();
	r->SetHighQualityVao(OpenGL::GetLargeSphereVao());
	r->SetLowQualityVao(OpenGL::GetSmallSphereVao());
	r->SetPosition(glm::vec3(130.0f, -30.0f, 130.0f));
	r->SetRadius(20.0f);
	r->SetParent(CTree::getInstance()->GetRoot());
	r->SetScale(20.0f);
	r->SetAllowObjectTextures(false);

	this->btab = new PointLight*[swiatelka];

	for (unsigned int i = 0; i < swiatelka; ++i)
	{
		this->btab[i] = NULL;
	}

	int x = 0;
	int y = 0;

	dl = new DirectionalLight();
	dl->SetDirection(glm::vec3(0.0f, -1.0f, 0.0f));
	dl->SetParent(CTree::getInstance()->GetRoot());
	dl->SetAmbient(Color(0.5f, 0.5f, 0.5f));
	dl->SetDiffuse(Color(0.5f, 0.5f, 0.5f));//Color(0.2f, 0.2f, 0.2f));
	dl->SetSpecular(vl::White);//Color(0.9f, 0.9f, 0.9f));

	dl->SetName("directionallight0");

// 	RenderableVao* r = new RenderableVao();
// 	r->SetName("plane");
// 	r->SetPosition(glm::vec3(0.0f, -100.0f, 0.0f));
// 	r->SetRadius(1000.0f);
// 	r->SetParent(CTree::getInstance()->GetRoot());
// 	r->SetLowQualityVao(CPrimitive::getInstance()->GetPlaneVao());
// 	r->SetHighQualityVao(CPrimitive::getInstance()->GetPlaneVao());
// 	r->SetScale(1000.0f);
// 	r->SetAllowObjectTextures(false);

// 	sl = new SpotLight();
// 	sl->SetAngleCos(0.3f);
// 	sl->SetParent(CTree::getInstance()->GetRoot());
// 	sl->SetAmbient(vl::Red);
// 	sl->SetDiffuse(vl::Red);
// 	sl->SetSpecular(vl::Red);
// 	sl->SetRadius(200.0f);
// 	sl->SetPosition(glm::vec3(200.0f, -100.0f, 200.0f));
// 	sl->SetName("spotlight0");
// 	sl->SetDirection(glm::vec3(1.0f, 0.0f, 0.0f));

	for (unsigned int i = 0; i < swiatelka; ++i)
	{
		std::stringstream ss;
		ss << "pointlight" << i;

		this->btab[i] = new PointLight();
		this->btab[i]->SetName(ss.str());
		this->btab[i]->SetParent(CTree::getInstance()->GetRoot());
		this->btab[i]->SetSpecular(Color(1.0f, 1.0f, 1.0f, 1.0f));
		this->btab[i]->SetAmbient(Color(rand() % 256, rand() % 256, rand() % 256, 255));
		this->btab[i]->SetDiffuse(this->btab[i]->GetAmbient());
		this->btab[i]->SetRadius(10.0f);
		this->btab[i]->SetPosition(glm::vec3(x * 50.0f, 0.0f, y * 50.0f));
		//this->btab[i]->SetForceRendering(true);
		x++;

		if (x == 6)
		{
			y++;
			x = 0;
		}

	}

	CTree::getInstance()->SetActiveObject(CTree::getInstance()->GetRoot());

	this->SetCalcStatus(true);
	//this->SetCalcInterval(1000.0f);
	//this->a = new Texture(800, 600);
	//CResources::getInstance()->Get("default", &this->a);
	CResources::getInstance()->Get("default", &this->fnt);

	//this->btab[0]->SetGodraysColor(vl::White);
	//PostprocessManager::getInstance()->SetGodRaysSource(this->btab[0]);

	//PostprocessManager::getInstance()->SetGodRaysSource(this->btab[0]);

	// 	via->Render()->Core()->SetMode(vl::Render::MODE_2D);
//
//
	//via->Render()->Core()->SetRenderTarget(NULL);*/
//
// 	Curve *c = new Curve(1.0f, true);
// 	FILL_CURVE(c, sin(v/vl::Pi) * 45.0f + 45.0f);
//
// 	CCamera::getInstance()->GetActive()->SetFieldOfView(c->

// 	this->AddRenderTask(CContext::getInstance()->GetContext("d2d"),
// 	                    CMaterial::getInstance()->GetNullMaterial(),
// 	                    0,
// 						vl::Render::HUD | vl::Render::COLOR_BUFFER
// 						);

// 	this->AddRenderTask(CContext::getInstance()->GetContext("d3d"),
// 	                    CMaterial::getInstance()->GetNullMaterial(),
// 	                    1,
// 						vl::Render::DEPTH_BUFFER | vl::Render::COLOR_BUFFER);*/
//
// 	Mesh* m = new Mesh("../Data/model/house5.3ds");
// 	m->SetForceRendering(true);
// 	m->SetScale(40.0f);
// 	m->SetParent(CTree::getInstance()->GetRoot());

// 	IParticlesEmitterObject* part = new IParticlesEmitterObject();
// 	part->SetParent(CTree::getInstance()->GetRoot());
// 	part->SetDirection(glm::vec3(0.0f, 1.0f, 0.0f));
// 	part->SetPosition(glm::vec3(0.0f, 10.0f, 0.0f));
//
// 	part->SetFrequency(20.0f);

	sound = NULL;
	kanon = NULL;

}

/*--------------------------------------------------------------------------*/

SimpleGame::~SimpleGame()
{
	//delete this->ball1;
}

/*--------------------------------------------------------------------------*/

void SimpleGame::OnCalc(unsigned int threadId)
{
	if (this->sound == NULL)
	{
		CResources::getInstance()->Get("beep", &sound);
	}

	if (this->kanon == NULL)
	{
		CResources::getInstance()->Get("kanon", &kanon);
	}

	if (CKeyboard::getInstance()->GetKeyState(vl::Key::k_6))
	{
		CMixer::getInstance()->PlayNow(sound);
	}

	if (CKeyboard::getInstance()->GetKeyState(vl::Key::k_7))
	{
		CMixer::getInstance()->PlayNow(kanon);
		CKeyboard::getInstance()->SetKeyState(vl::Key::k_7, false);
	}


	float time = (float) CSdl::getInstance()->GetTicks();

	for (int i = 0; i < swiatelka; ++i)
	{
		this->btab[i]->SetPosition(glm::vec3(120.0f, 0.0f, 120.0f) - glm::vec3(sinf(i * time / 100000.0f) * i  * 130.0f / ((float) swiatelka), 16.0f, cosf(i * time / 100000.0f) * i  * 130.0f / ((float) swiatelka)));
	}

	if (CKeyboard::getInstance()->GetKeyState(vl::Key::k_p))
	{
		std::stringstream ss;
		ss << "echo >>>";

		for (int i = 0; i < 10; ++i)
		{
			ss << "  " << this->walls[i * 10]->GetOcclusionTestResult() << this->walls[i * 10]->GetFrustumTestResult() << "#" << this->walls[i * 10]->GetOcclusionTestResult();
		}

		CShell::getInstance()->Execute(ss.str());
		//CSdl::getInstance()->AddEventToNextFrame(ne);
	}

	this->BindRenderTasks();

	if (CKeyboard::getInstance()->GetKeyState(vl::Key::k_x))
	{
		for (unsigned int i = 0; i < swiatelka; ++i)
		{
			if (this->btab[i] != NULL)
			{
				this->btab[i]->SetRadius(this->btab[i]->GetRadius() + 0.1f);
			}
		}
	}

	if (CKeyboard::getInstance()->GetKeyState(vl::Key::k_c))
	{
		for (unsigned int i = 0; i < swiatelka; ++i)
		{
			if (this->btab[i] != NULL)
			{
				this->btab[i]->SetRadius(this->btab[i]->GetRadius() - 0.1f);
			}
		}
	}

//
// 	if (CKeyboard::getInstance()->GetKeyState(vl::Key::k_1))
// 	{
// 		sl->SetAngleCos(sl->GetAngleCos() + 0.01f);
// 	}
//
// 	if (CKeyboard::getInstance()->GetKeyState(vl::Key::k_2))
// 	{
// 		sl->SetAngleCos(sl->GetAngleCos() - 0.01f);
// 	}

// 	if (CKeyboard::getInstance()->GetKeyState(vl::Key::k_0))
// 	{
// 		std::stringstream ss;
// 		ss << "echo pos=" << sl->GetPosition() << " angle=" << sl->GetAngleCos() << " dir=" << sl->GetDirection() << " rad=" << sl->GetRadius();
// 		CShell::getInstance()->Execute(ss.str());
// 	}

	//CCamera::getInstance()->GetActive()->SetFieldOfView(sinf(CSdl::getInstance()->GetTicks() / 1000.0f) * 30.0f + 60.0f);

	//Vector3f b1;// = this->ball1->GetRotation();

// 	b1(1) = sinf(CSdl::getInstance()->GetTicks() / 10000.0f) * 360.0f;
// 	b1(0) = cosf(CSdl::getInstance()->GetTicks() / 15000.0f) * 360.0f;
// 	b1(2) = cosf(CSdl::getInstance()->GetTicks() / 5000.0f) * 360.0f;
// 	this->ball1->SetRotation(b1);

	//this->plane->SetRotation(Vector3f(0.0f, 0.0f, (float)CSdl::getInstance()->GetTicks() / 100.0f));
// 	b1 = this->ball2->GetPosition();
// 	b1(0) = sinf(CSdl::getInstance()->GetTicks() / this->var_d->GetFloat()) * 300.0f;
// 	b1(2) = cosf(CSdl::getInstance()->GetTicks() / this->var_d->GetFloat()) * 300.0f;
// 	b1(1) = 0.0f;
// 	this->ball2->SetPosition(b1);
//
// 		b1(0) = sinf(CSdl::getInstance()->GetTicks() / this->var_d->GetFloat()) * 150.0f + 150.0f;
// 		b1(2) = cosf(CSdl::getInstance()->GetTicks() / this->var_d->GetFloat()) * 150.0f + 150.0f;
// 		b1(1) = 40.0f;
// 		this->btab[0]->SetPosition(b1);
// 	Vector3f b1;
// 	b1(0) = sinf(CSdl::getInstance()->GetTicks() / this->var_d->GetFloat()) * 150.0f + 150.0f;
// 	b1(2) = 0.0f;
// 	b1(1) = 50.0f;//cosf(CSdl::getInstance()->GetTicks() / this->var_d->GetFloat()) * 100.0f;
// 	this->btab[1]->SetPosition(b1);
//
// 	b1(1) = 50.0f;//sinf(CSdl::getInstance()->GetTicks() / this->var_d->GetFloat()) * 100.0f;
// 	b1(0) = 0.0f;
// 	b1(2) = cosf(CSdl::getInstance()->GetTicks() / this->var_d->GetFloat()) * 150.0f + 150.0f;
// 	this->btab[2]->SetPosition(b1);


	//b1 = this->btab[0]->GetPosition();
//
//  	for (unsigned int i = 0; i < 30; ++i)
//  	{
// 		if(i%2)
// 		{
// 			this->btab[i]->SetPosition( Vector3f(  sin((float) CSdl::getInstance()->GetTicks() * 0.001f) * 2.0f * i + i%6 * 20,
// 				0.0f,
// 				-cos((float) CSdl::getInstance()->GetTicks() * 0.001f) * 2.0f * i + i%5 * 20) * 4.0f );
//
// 		}
// 		else
// 		{
// 			this->btab[i]->SetPosition( Vector3f(  sin((float) CSdl::getInstance()->GetTicks() * 0.001f) * 2.0f * i + i%6 * 20,
// 				0.0f,
// 				cos((float) CSdl::getInstance()->GetTicks() * 0.001f) * 2.0f * i + i%5 * 20) * 4.0f );
//
// 		}
//  	}

	return;

	if (CKeyboard::getInstance()->GetKeyState(vl::Key::k_t))
	{
		if (CKeyboard::getInstance()->GetKeyState(vl::Key::k_o))
		{
			sl->SetDirection(sl->GetDirection() + glm::vec3(1.0f, 0.0f, 0.0f));
		}

		if (CKeyboard::getInstance()->GetKeyState(vl::Key::k_p))
		{
			sl->SetDirection(sl->GetDirection() + glm::vec3(-1.0f, 0.0f, 0.0f));
		}

		if (CKeyboard::getInstance()->GetKeyState(vl::Key::k_k))
		{
			sl->SetDirection(sl->GetDirection() + glm::vec3(0.0f, 1.0f, 0.0f));
		}

		if (CKeyboard::getInstance()->GetKeyState(vl::Key::k_l))
		{
			sl->SetDirection(sl->GetDirection() + glm::vec3(0.0f, -1.0f, 0.0f));
		}

		if (CKeyboard::getInstance()->GetKeyState(vl::Key::k_n))
		{
			sl->SetDirection(sl->GetDirection() + glm::vec3(0.0f, 0.0f, 1.0f));
		}

		if (CKeyboard::getInstance()->GetKeyState(vl::Key::k_m))
		{
			sl->SetDirection(sl->GetDirection() + glm::vec3(0.0f, 0.0f, -1.0f));
		}
	}
	else
	{
		if (CKeyboard::getInstance()->GetKeyState(vl::Key::k_o))
		{
			sl->SetPosition(sl->GetPosition() + glm::vec3(1.0f, 0.0f, 0.0f));
		}

		if (CKeyboard::getInstance()->GetKeyState(vl::Key::k_p))
		{
			sl->SetPosition(sl->GetPosition() + glm::vec3(-1.0f, 0.0f, 0.0f));
		}

		if (CKeyboard::getInstance()->GetKeyState(vl::Key::k_k))
		{
			sl->SetPosition(sl->GetPosition() + glm::vec3(0.0f, 1.0f, 0.0f));
		}

		if (CKeyboard::getInstance()->GetKeyState(vl::Key::k_l))
		{
			sl->SetPosition(sl->GetPosition() + glm::vec3(0.0f, -1.0f, 0.0f));
		}

		if (CKeyboard::getInstance()->GetKeyState(vl::Key::k_n))
		{
			sl->SetPosition(sl->GetPosition() + glm::vec3(0.0f, 0.0f, 1.0f));
		}

		if (CKeyboard::getInstance()->GetKeyState(vl::Key::k_m))
		{
			sl->SetPosition(sl->GetPosition() + glm::vec3(0.0f, 0.0f, -1.0f));
		}
	}


//	this->btab[0]->SetPosition(b1);

// 	Timer t;
// 	Vector4f q[10];
// 	std::stringstream ss;
// 	float w;
// 	ss << "echo ";
// 	t.SetTimer();
//
// 	for (int i = 0; i < 1000; ++i)
// 	{
// 		SSEMath::sqrt(q[i % 10]);
// 	}
//
// 	w = t.GetTimer();
// 	ss << w << " ";
//
// 	t.SetTimer();
//
// 	for (int i = 0; i < 1000; ++i)
// 	{
// 		q[i % 10](0) = sqrtf(q[i % 10](0));
// 		q[i % 10](1) = sqrtf(q[i % 10](1));
// 		q[i % 10](2) = sqrtf(q[i % 10](2));
// 		q[i % 10].w = sqrtf(q[i % 10].w);
// 	}
//
// 	w = t.GetTimer();
// 	ss << w << " ";
//
// 	CShell::getInstance()->Execute(ss.str());
}

/*--------------------------------------------------------------------------*/

void SimpleGame::OnRender(const unsigned int i, vl::Render::Quality quality)
{

}

/*--------------------------------------------------------------------------*/